/*
 ** Author: Winys
 ** Time: 2015.4.9
 *  对所有data-submit属性绑定事件
 */

//Verify.js 用于验证表单字符串 支持 “！” 语句

//常用Reg库
$.Reg = {

	IP : /^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$/,
	NULL : /^$/

}


var Verify = function () {
}

//外部调用模式$.verify_test
$.verify_test = Verify.test = function (type,value){
	if(!type)return true;
	//以空格隔开，可以使用多个验证
	var typelist = type.split(/\s* \s*/),
		status = true
		;
	for ( var key in typelist ){
		var TYPE = typelist[key].toUpperCase();
		if(  TYPE.indexOf('!') == 0 ){
			TYPE = TYPE.slice(1);
			if($.Reg[TYPE])
				status = status && !$.Reg[TYPE].test(value);
		}
		else{
			if( $.Reg[TYPE] )
				status = status && $.Reg[TYPE].test(value);
		}
	}

	return status;
}

//表单焦点触发验证
$(document).on("blur", "[data-verify]",function (event) {
	var $this = $(this).closest("[data-verify]"),
		$submit = $this.closest("form").find("[data-submit]"),
		type = $this.data("verify"),
		val = $this.val()
		;

	if(Verify.test(type,val)){
		$this.tooltip('destroy');
		$this.removeClass("verify_err");
		$submit.data("err",$submit.data("err")?parseInt($submit.data("err"))-1:0);
		return;
	}

	$this.tooltip({
		title:"格式不正确",
		placement:"bottom",
		trigger:"manual"
	});
	$this.tooltip('show');
	$this.addClass("verify_err");
	$submit.data("err",$submit.data("err")?parseInt($submit.data("err"))+1:1);
});